package com.ruoyi.recycle.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.CommentManagement;
import com.ruoyi.recycle.domain.ConsultManagement;
import com.ruoyi.recycle.domain.IntegralCommodity;
import com.ruoyi.recycle.domain.SysTypeConfig;
import com.ruoyi.recycle.dto.ConsultManagementDTO;
import com.ruoyi.recycle.mapper.CommentManagementMapper;
import com.ruoyi.recycle.mapper.ConsultManagementMapper;
import com.ruoyi.recycle.mapper.SysTypeConfigMapper;
import com.ruoyi.recycle.service.IConsultManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 咨询管理Service业务层处理
 *
 * @author Wubk
 * @date 2022-05-22
 */
@Service
public class ConsultManagementServiceImpl extends ServiceImpl<ConsultManagementMapper, ConsultManagement> implements IConsultManagementService {
    @Autowired
    private ConsultManagementMapper consultManagementMapper;
    @Resource
    private CommentManagementMapper commentManagementMapper;
    @Resource
    private SysTypeConfigMapper sysTypeConfigMapper;

    /**
     * 查询咨询管理
     *
     * @param id 咨询管理主键
     * @return 咨询管理
     */
    @Override
    public ConsultManagement selectConsultManagementById(Long id) {
        return consultManagementMapper.selectConsultManagementById(id);
    }

    /**
     * 查询咨询管理列表
     *
     * @param consultManagement 咨询管理
     * @return 咨询管理
     */
    @Override
    public List<ConsultManagement> selectConsultManagementList(ConsultManagement consultManagement) {
        return consultManagementMapper.selectConsultManagementList(consultManagement);
    }

    /**
     * 新增咨询管理
     *
     * @param consultManagement 咨询管理
     * @return 结果
     */
    @Override
    public int insertConsultManagement(ConsultManagement consultManagement) {
        return consultManagementMapper.insertConsultManagement(consultManagement);
    }

    /**
     * 修改咨询管理
     *
     * @param consultManagement 咨询管理
     * @return 结果
     */
    @Override
    public int updateConsultManagement(ConsultManagement consultManagement) {
        return consultManagementMapper.updateConsultManagement(consultManagement);
    }

    /**
     * 批量删除咨询管理
     *
     * @param ids 需要删除的咨询管理主键
     * @return 结果
     */
    @Override
    public int deleteConsultManagementByIds(Long[] ids) {
        return consultManagementMapper.deleteConsultManagementByIds(ids);
    }

    /**
     * 删除咨询管理信息
     *
     * @param id 咨询管理主键
     * @return 结果
     */
    @Override
    public int deleteConsultManagementById(Long id) {
        return consultManagementMapper.deleteConsultManagementById(id);
    }

    @Override
    public AjaxResult selectMiniConsultManagementList(ConsultManagement consultManagement) {
        LambdaQueryWrapper<ConsultManagement> queryWrapper = Wrappers.lambdaQuery(consultManagement);
        queryWrapper.orderByDesc(ConsultManagement::getCreatedTime);
        List<ConsultManagement> consultManagements = consultManagementMapper.selectList(queryWrapper);
        String oldOb = JSON.toJSONString(consultManagements);
        List<ConsultManagementDTO> consultManagementDTOS = JSON.parseArray(oldOb, ConsultManagementDTO.class);
        for (ConsultManagementDTO consultManagementDTO : consultManagementDTOS) {
            List<CommentManagement> commentManagements = commentManagementMapper.selectList(new LambdaQueryWrapper<CommentManagement>().
                    eq(CommentManagement::getConsultId, consultManagementDTO.getId()).eq(CommentManagement::getStatus,"1"));
            consultManagementDTO.setCommentManagementList(commentManagements);
        }

        List<SysTypeConfig> sysTypeConfigList = sysTypeConfigMapper.selectList(new LambdaQueryWrapper<SysTypeConfig>().eq(SysTypeConfig::getType, "好玩分类"));

        return AjaxResult.success(consultManagementDTOS).put("tab",sysTypeConfigList);
    }
}
